<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索功能 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表表格区域 -->
      <el-table border :data="orderList" style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template v-slot:default="scope">
            <el-tag type="success" v-if="scope.row.pay_status === 0">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货">
          <template v-slot:default="scope">
            <el-tag type="danger">{{ scope.row.is_send === 0 ? '未发货' : '已发货' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">{{ scope.row.create_time | dateFormat }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <el-button type="primary" icon="el-icon-edit" @click="editAddr"></el-button>
            <el-button type="success" icon="el-icon-location-outline" @click="showWuliuInfo"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="orderForm.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="orderForm.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="addressVisible" width="50%" @close='addressFormDialogClose'>
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="addressForm.address1"
            :options="citydata"
            :props="{ expandTrigger: 'hover' }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流信息对话框 -->
    <el-dialog title="物流信息" :visible.sync="wuliuDialogVisible" width="50%">
      <!-- 时间线 -->
      <el-timeline :reverse="reverse">
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import citydata from './catydata.js'
export default {
  data() {
    return {
      orderForm: {
        query: '',
        pagenum: 1,
        pagesize: 5,
        user_id: '',
        pay_status: '',
        is_send: '',
        order_fapiao_title: '',
        order_fapiao_company: '',
        order_fapiao_content: '',
        consignee_addr: ''
      },
      orderList: [],
      total: 0,
      // 控制修改地址对话框的显示 隐藏
      addressVisible: false,
      addressForm: {
        address1: [],
        address2: ''
      },
      citydata,
      // 地址验证规则
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区 县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      // 控制物流信息对话框的显示和隐藏
      wuliuDialogVisible: false,
      reverse: true,
      // 物流信息
      progressInfo: []
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 获取订单列表
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.orderForm
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败')
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 监听页面显示大小改变时触发
    handleSizeChange(val) {
      this.orderForm.pagesize = val
      this.getOrderList()
    },
    // 监听页面页码改变时触发
    handleCurrentChange(val) {
      console.log(val)
      this.orderForm.pagenum = val
      this.getOrderList()
    },
    // 修改发货地址
    editAddr() {
      this.addressVisible = true
    },
    // 监听修改地址对话框的关闭事件
    addressFormDialogClose() {
      this.$refs.addressFormRef.resetFields()
    },
    handleChange() {},
    // 显示物流信息对话框
    async showWuliuInfo() {
      const { data: res } = await this.$http.get('http://localhost:8888/kuaidi/SF1018492702788')
      // const res = {
      //   data: [
      //     {
      //       time: '2020-05-06 10:10:20',
      //       ftime: '2020-4-10 09:39:20',
      //       context: '您的快件已暂存至郑州东风路32号院11号楼菜鸟驿站，请凭取件码及时领取。如有疑问请联系15517598573',
      //       location: ''
      //     },
      //     {
      //       time: '2020-05-06 08:29:20',
      //       ftime: '2020-4-10 05:06:20',
      //       context: '【郑州市】 河南郑州公司南阳路亚新分部派件员：路杰 电话：17050340037 当前正在为您派件',
      //       location: ''
      //     },
      //     {
      //       time: '2020-05-05 18:49:20',
      //       ftime: '2020-4-10 18:49:20',
      //       context: '已到达 河南郑州公司南阳路亚新分部',
      //       location: ''
      //     },
      //     {
      //       time: '2020-05-05 17:07:20',
      //       ftime: '2020-4-10 17:07:20',
      //       context: '已离开 河南郑州公司；发往 河南郑州南阳路亚新分部',
      //       location: ''
      //     },
      //     {
      //       time: '2020-05-05 13:24:20',
      //       ftime: '2020-4-10 17:07:20',
      //       context: '已离开 河南郑州分拨中心；发往郑州公司',
      //       location: ''
      //     },
      //     {
      //       time: '2020-05-05 13:19:20',
      //       ftime: '2020-4-10 09:39:20',
      //       context: '已到达 郑州分拨中心',
      //       location: ''
      //     },
      //     {
      //       time: '2020-05-04 23:56:20',
      //       ftime: '2020-4-10 09:39:20',
      //       context: '已离开 陕西西安分拨中心；发往郑州公司',
      //       location: ''
      //     },
      //     {
      //       time: '2020-05-04 23:55:20',
      //       ftime: '2020-4-10 09:39:20',
      //       context: '【西安市】已到达 陕西西安分拨中心',
      //       location: ''
      //     },
      //     {
      //       time: '2020-05-04 18:11:20',
      //       ftime: '2020-4-10 09:39:20',
      //       context: '【西安市】 陕西西安高新二公司 已揽收',
      //       location: ''
      //     },
      //     {
      //       time: '2020-05-04 10:35:20',
      //       ftime: '2020-4-10 09:39:20',
      //       context: '已到达 包裹正在等待揽收',
      //       location: ''
      //     },
      //     {
      //       time: '2020-05-04 10:35:20',
      //       ftime: '2020-4-10 09:39:20',
      //       context: '商品已下单',
      //       location: ''
      //     }
      //   ]
      // }
      console.log(res)
      this.progressInfo = res.data
      this.wuliuDialogVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-pagination {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
